/////////////////////////////////////////////////////////////////////////
//Module Name:
//
//    CANDll.h  
//
//Abstract:   CAN interface.
//
//Environment:
//
//    Windows2000/Windows XP
//
//
/////////////////////////////////////////////////////////////////////////

#include <windows.h>

#ifndef _CANDll_H_
#define _CANDll_H_

#define  PLK_SUCCESS				0
#define  PLK_UNSUCCESS				-1

/////////////////////////////////////////////////////////////////////////
//
// constant define .
//
/////////////////////////////////////////////////////////////////////////


#define NO_ACTION                                  255
                                                   
// CAN work mode,"BasicCAN" or "PeliCAN"           
#define BASICCAN_MODE                              1
#define PELICAN_MODE                               2
                                                   
// sleep or wake up                                
#define WAKE_UP_MODE                               1
#define SLEEP_MODE                                 2
                                                   
// "PeliCAN" mode                                  
#define DUAL_ACC_FILTER_MODE                       0
#define SINGLE_ACC_FILTER_MODE                     1
#define SELF_TEST_MODE                             1
#define LISTEN_ONLY_MODE                           1
                                                   
// baud rate                                       
#define    BAUD_RATE_5K                            5
#define    BAUD_RATE_10K                           10
#define    BAUD_RATE_20K                           20
#define    BAUD_RATE_40K                           40
#define    BAUD_RATE_50K                           50
#define    BAUD_RATE_80K                           80
#define    BAUD_RATE_100K                          100
#define    BAUD_RATE_125K                          125
#define    BAUD_RATE_200K                          200
#define    BAUD_RATE_250K                          250
#define    BAUD_RATE_400K                          400
#define    BAUD_RATE_500K                          500
#define    BAUD_RATE_666K                          666
#define    BAUD_RATE_800K                          800
#define    BAUD_RATE_1000K                         1000
                                                   
// default OCR                                     
#define DEFAULT_OUT_CONTROL                        0x1a
                                                   
// transmit type                                   
#define TRANSMISSION_REQUEST                       1
#define SINGLE_SHOT_REQUEST                        2
#define SELF_RECEIPTION_REQUEST                    3
#define SINGLE_SHOT_AND_SELF_RECEIPTION_REQUEST    4

// frame format
#define STAANDARD_FRAME                            1
#define EXTEND_FRAME                               2
                                                   
// frame type                                      
#define DATA_FRAME                                 1
#define REMOTE_FRAME                               2
                                                   
// buffer type                                     
#define TRANSMIT_BUFFER                            1
#define RECEIVE_BUFFER                             2
                                                   
// error type                                      
#define ERROR_WARNING                              1
#define ERROR_PASSIVE                              2
#define BUS_OFF                                    3
#define ARBITRATION_LOST                           4
#define DATA_OVERRUN                               5


/////////////////////////////////////////////////////////////////////////
//
// structure.
//
////////////////////////////////////////////////////////////////////////

// CAN init information structure.
// Members:
//        Mode1        -    It corresponding the constant as
//                            BASICCAN_MODE : "BasicCAN".
//                            PELICAN_MODE  : "PeliCAN".
//        Mode2        -    It corresponding the constant as
//        BaudRate
//        Filter
//        AccCode
//        AccMask
//        OutControl
//
typedef struct _CAN_INIT_INFO {
    UCHAR    Mode;                  // "BasicCAN" or "PeliCAN".
    UCHAR    SleepFlag;             // sleep,self test,listen only,reset
    UCHAR    FilterFlag;            // filter mode,only for "PeliCAN",in "BasicCAN" is invalid.
    UCHAR    SelfTestFlag;          //
    UCHAR    ListenOnlyFlag;        //
    USHORT    BaudRate;             // baud rate.when this parameters set to "0",BTR0,BTR1 will be used.
    UCHAR    BTR0;                  // bus timing 0.this parameters is valid when "BaudRate" set to "0",otherwise,is invalid.
    UCHAR    BTR1;                  // bus timing 1.this parameters is valid when "BaudRate" set to "0",otherwise,is invalid.
    ULONG    AccCode;               // acceptance code.
    ULONG    AccMask;               // acceptance mask.
    UCHAR    OutControl;            // output control.
    ULONG    TransBufferSize;       // transmit buffer size,as frame numbers.
    ULONG    RecBufferSize;         // receive buffer size,as frame numbers.
} CAN_INIT_INFO, *PCAN_INIT_INFO;

// Transmit or receive data information structure.
// Members:
//        TransmitType
//        FrameFormat
//        FrameType
//        ID
//        lpBuffer
//        DataLength
//        
typedef struct _CAN_DATA_INFO {
    UCHAR    TransmitType;          // transmit type.
    UCHAR    FrameFormat;           // frame format.
    UCHAR    FrameType;             // frame type.
    ULONG    ID;                    // frame ID.
    PUCHAR    LPBuffer;             // transmit: point to the data need transmit.
                                    // receive: point to save data buffer.  
    DWORD    DataLength;            // transmit: transmit data length.
                                    // receive: save data buffer length,when data received,
                                    //            modified as infact data length received.  
} CAN_DATA_INFO, *PCAN_DATA_INFO;

// CAN Error information structure.
// Members:
//    
//
// 
typedef struct _CAN_ERROR_INFO {
    UCHAR    ErrType;               // error type.
    UCHAR    ErrCapCode;            // error capture code.
    UCHAR    ArLostErrData;         // arbitration lost capture code.
    UCHAR    RxErrCounter;          // RX error counter.
    UCHAR    TxErrCounter;          // TX error counter.
} CAN_ERROR_INFO,*PCAN_ERROR_INFO;

// CAN status information structure.
// Members:
//
//
//
typedef struct _CAN_STATUS_INFO {
    UCHAR    RegMode;               // mode register.
    UCHAR    RegStatus;             // staus register.
    UCHAR    RegALCapture;          // arbitration lost capture register.
    UCHAR    RegECCapture;          // error code capture register.
    UCHAR    RegEWLimit;            // error warning limit register.
    UCHAR    RegRECounter;          // RX error counter.
    UCHAR    RegTECounter;          // TX error counter.
} CAN_STATUS_INFO,*PCAN_STATUS_INFO;

/////////////////////////////////////////////////////////////////////////
//Function Description:
//    open CAN board device.
//
//Parameters:
//
//    device    - CAN board number,start with 0,as 0,1,2...
//
//Return Value:
//     0         - CAN board device open successed.
//    -1         - CAN board device open failed.
//            
/////////////////////////////////////////////////////////////////////////
ULONG __stdcall DeviceOpen(ULONG device );

/////////////////////////////////////////////////////////////////////////
//Function Description:
//    close CAN board device.
//
//Parameters:
//
//    device    - CAN board number,start with 0,as 0,1,2...
//
//Return Value:
//     0         - CAN board device open successed.
//    -1         - CAN board device open failed.
//            
/////////////////////////////////////////////////////////////////////////
void __stdcall DeviceClose(ULONG device );


/////////////////////////////////////////////////////////////////////////
//Function Description:
//    Init CAN controller.
//
//Parameters:
//    device             - DeviceOpen() open successed the number
//    ChannelsIndex      - CAN channels number,from 0 to 5
//    pCANInitInfo       - Point to CAN a CAN_INIT_INFO structure.
//
//Return Value:
//     0        - Init CAN controller successed.
//    -1        - Init CAN controller failed.
//            
/////////////////////////////////////////////////////////////////////////
ULONG __stdcall CANInit(ULONG device,ULONG ChannelsIndex,PCAN_INIT_INFO pCANInitInfo);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//    Get CAN controller reference.
//
//Parameters:
//    device             - DeviceOpen() open successed the number
//    ChannelsIndex      - CAN channels number,from 0 to 5
//    CANAddress         - CAN controller register address.
//    pValue             - buffer which save register value.
//
//Return Value:
//     0        - get reference successed.
//    -1        - get reference failed.
//            
/////////////////////////////////////////////////////////////////////////
ULONG __stdcall CANGetRegister(ULONG device,ULONG ChannelsIndex,unsigned short CANAddress,byte *pValue);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//    Set CAN controller reference.
//
//Parameters:
//    device             - DeviceOpen() open successed the number
//    ChannelsIndex      - CAN channels number,from 0 to 5
//    CANAddress         - CAN controller register address.
//    Value              - register value need set.
//
//Return Value:
//     0        - set reference successed.
//    -1        - set reterence failed.
//            
/////////////////////////////////////////////////////////////////////////
ULONG __stdcall CANSetRegister(ULONG device,ULONG ChannelsIndex,unsigned short CANAddress,byte Value);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//    Reset CAN.
//
//Parameters:
//    device             - DeviceOpen() open successed the number
//    ChannelsIndex      - CAN channels number,from 0 to 5
//
//Return Value:
//     0           - Reset CAN successed.
//    -1           - Reset CAN failed.
//            
/////////////////////////////////////////////////////////////////////////
ULONG __stdcall CANReset(ULONG device,ULONG ChannelsIndex);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//    Transmit data.
//
//Parameters:
//    device             - DeviceOpen() open successed the number
//    ChannelsIndex      - CAN channels number,from 0 to 5
//    pCANDataInfo       - Point to CAN a CAN_DATA_INFO structure.
//    Length             - PCAN_DATA_INFO array length.
//
//Return Value:
//  0xffffffff          - transmit data failed.
//    others            - Infact frame numbers to transmited.
//            
/////////////////////////////////////////////////////////////////////////
DWORD __stdcall CANTransmit(ULONG device,ULONG ChannelsIndex,PCAN_DATA_INFO pTransCANDataInfo,DWORD Length);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//    Receive data.
//
//Parameters:
//    device             - DeviceOpen() open successed the number
//    ChannelsIndex      - CAN channels number,from 0 to 5
//    pCANDataInfo      - Point to CAN a CAN_DATA_INFO structure.
//    Length            - PCAN_DATA_INFO array length.
//    WaitTime          - if no data in driver buffer at this moment,function wait for 
//                        "WaitTime" millisecond.then no data in driver buffer,
//                        this funtion will be return.
//
//Return Value:
//    0xffffffff        - receiceive data failed.
//    others            - Infact numbers of bytes to received.
//            
/////////////////////////////////////////////////////////////////////////
DWORD __stdcall CANReceive(ULONG device,ULONG ChannelsIndex,PCAN_DATA_INFO pRcvCANDataInfo,DWORD Length,unsigned long WaitTime);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//    Read last error infomation.
//
//Parameters:
//    device             - DeviceOpen() open successed the number
//    ChannelsIndex      - CAN channels number,from 0 to 5
//    pCANErrorInfo      - Point to CAN a CAN_ERROR_INFO structure.
//
//Return Value:
//    0         - Read last error information successed.
//    -1        - Read last error information failed.
//            
/////////////////////////////////////////////////////////////////////////
ULONG __stdcall CANGetLastErrorInfo(ULONG device,ULONG ChannelsIndex,PCAN_ERROR_INFO pCANErrorInfo);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//    Read CAN controller current status infomation.
//
//Parameters:
//    device             - DeviceOpen() open successed the number
//    ChannelsIndex      - CAN channels number,from 0 to 5
//    pCANStatusInfo     - Point to CAN a CAN_STATUS_INFO structure.
//
//Return Value:
//    0         - Read CAN controller current status infomation successed.
//    -1        - Read CAN controller current status infomation failed.
//            
/////////////////////////////////////////////////////////////////////////
ULONG __stdcall CANGetStatusInfo(ULONG device,ULONG ChannelsIndex,PCAN_STATUS_INFO pCANStatusInfo);

#endif